<?php

/**
 * Compras filter form.
 *
 * @package    tercermilenio
 * @subpackage filter
 * @author     Your name here
 */
class ComprasFormFilter extends BaseComprasFormFilter {

    public function configure() {
        $this->widgetSchema['cpr_fecha'] = new sfWidgetFormFilterDate(
                        array(
                            'from_date' => new sfWidgetFormJQueryDate(array('format' => '%day%/%month%/%year%')),
                            'to_date' => new sfWidgetFormJQueryDate(array('format' => '%day%/%month%/%year%')),
                            'with_empty' => false,
                ));
        $this->widgetSchema['cpr_fecha']->addOption('culture', 'es');
        $this->widgetSchema['cpr_fecha']->addOption('template', 'Desde %from_date%<br/>Hasta %to_date%');
    }

    public function setup() {
        parent::setup();
        $this->setWidgets(array(
            'cpr_nro_factura' => new sfWidgetFormFilterInput(array('with_empty' => false)),
            'cpr_proveedor' => new sfWidgetFormPropelChoice(array('model' => 'Proveedores', 'add_empty' => 'Seleccione un proveedor')),
            'cpr_fecha' => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDateJQueryUI(), 'to_date' => new sfWidgetFormDateJQueryUI(), 'with_empty' => false)),
            'cpr_usuario' => new sfWidgetFormPropelChoice(array('model' => 'sfGuardUser', 'add_empty' => 'Seleccione un usuario')),
                //'cpr_total' => new sfWidgetFormFilterInput(),
                //'detalle_compras_list' => new sfWidgetFormPropelChoice(array('model' => 'Productos', 'add_empty' => true)),
        ));

        $this->setValidators(array(
            'cpr_nro_factura' => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
            'cpr_proveedor' => new sfValidatorPropelChoice(array('required' => false, 'model' => 'Proveedores', 'column' => 'pvd_id')),
            'cpr_fecha' => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))),
            'cpr_usuario' => new sfValidatorPropelChoice(array('required' => false, 'model' => 'Usuarios', 'column' => 'usu_id')),
                //'cpr_total' => new sfValidatorSchemaFilter('text', new sfValidatorNumber(array('required' => false))),
                //'detalle_compras_list' => new sfValidatorPropelChoice(array('model' => 'Productos', 'required' => false)),
        ));

        $this->widgetSchema->setNameFormat('compras_filters[%s]');

        $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
    }

    public function addCprNroFacturaColumnCriteria(Criteria $criteria, $field, $values) {

        if (!empty($values['text'])) {

            $column = $this->getColName($field);
            $criteria->add($column, '%' . $values['text'] . '%', Criteria::LIKE);
        }
    }

    public function addCprFechaColumnCriteria(Criteria $criteria, $field, $values) {

            $inicioDia = '00:00:00';
            $finDia = '23:59:59';

        if (!empty($values['from'])) {
            $filterInicio = $values['from'] . ' ' . $inicioDia;
            $criteria->add(ComprasPeer::CPR_FECHA, $filterInicio, Criteria::GREATER_EQUAL);
        }
        
        if (!empty($values['to'])) {           
            $filterFin = $values['to'] . ' ' . $finDia;         
            $criteria->add(ComprasPeer::CPR_FECHA, $filterFin, Criteria::LESS_EQUAL);
        }
    }

}
